using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Fieldbook.Business;
using Fieldbook.Entities;
using System.Collections.Generic;

namespace Fieldbook.Testwebsite.User
{
    public partial class OperationsOverview : FieldbookPage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            ReddirectNonActiveSession();
        }

        public void LoadOperations()
        {
            if (dgvOperationCategories.SelectedIndex != -1)
            {
                long userID = (long)Session["userID"];
                long categoryID = Int64.Parse(dgvOperationCategories.SelectedDataKey.Value.ToString());
                List<Operation> items = OperationController.LoadOperations(userID, categoryID);
                dgvOperations.DataSource = items;
                dgvOperations.DataBind();
            }
        }
        protected void DgvOperationCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadOperations();
        }

        /// <summary>
        /// Adding new operation category
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void linkAddCategory_Command(object sender, CommandEventArgs e)
        {
            bool sucess = false;
            if (e.CommandArgument.ToString() == "global")
            {
                sucess = OperationController.InsertOperationCategory(txtOperationCategory.Text);
            }
            else
            {
                long userID = (long)Session["userID"];
                sucess = OperationController.InsertOperationCategory(txtOperationCategory.Text, userID);
            }
            if (sucess)
            {
                this.lblOperationStatus.Text = "Category sucessfully added";
                this.txtOperationCategory.Text = "";
                this.dgvOperationCategories.DataBind();
                this.ddlOperationCategory.DataBind();
            }
            else
            {
                this.lblOperationStatus.Text = "Error adding category";
            }
        }

        /// <summary>
        /// Add new operation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void linkAddOperation_Command(object sender, CommandEventArgs e)
        {
            bool sucess = false;
            long userID = (long)Session["userID"];
            long operationCategory = Int64.Parse(ddlOperationCategory.SelectedValue);
            string description = txtDescription.Text;
            long? houseHoldID = AppUserController.GetHoueHoldID(userID);
            if (e.CommandArgument.ToString() == "global")
            {
                sucess = OperationController.InsertOperation(operationCategory, description, null, Operation.OperationType.RequestedService);
            }
            else if (houseHoldID != null)
            {
                sucess = OperationController.InsertOperation(operationCategory, description, houseHoldID, Operation.OperationType.RequestedService);
            }
            if (sucess)
            {
                lblOperationStatus.Text = "Operation sucessfully added";
                txtDescription.Text = "";
                this.dgvOperationCategories.DataBind();
                LoadOperations();
            }
            else
            {
                lblOperationStatus.Text = "Error adding Operation";
            }
        }
    }
}
